<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class RecruitmentModel extends CI_Model {

    public $table = 'recruitments';
    public $primary = 'recruitment_id';

    function add($data) {
        $this->db->insert($this->table, $data);
    }

    function update($id, $data) {
        $this->db->where($this->primary, $id);
        return $this->db->update($this->table, $data);
    }

    function getRecruitmentByUserId($user_id) {
        $query = $this->db->query("SELECT * 
  	    							FROM  `{$this->table}`
  	                                 WHERE `user_id` = {$user_id}");
        return $query->row_array();
    }

    public function getRecruitments($limit, $start) {
        $query = $this->db->query("SELECT * FROM `{$this->table}` AS r
  	                                INNER JOIN users AS u ON u.user_id = r.user_id
                            	     ORDER BY `{$this->primary}` DESC 
                            	      LIMIT {$start}, {$limit}");
        return $query->result_array();
    }

    /**
     * @view getCompanyBusiness() in frontend_helper.php
     */
    public function getRecruitmentsByCareer($limit, $start, $career) {
        // WHERE career IN (1,2,3,4);
        if ($career == 1) {
            $careerStr = 'IN (1,2,3,4,5,6,7)';
        } else if ($career == 8) {
            $careerStr = 'IN (8,9,10)';
        } else if ($career == 11) {
            $careerStr = 'IN (11,12,13)';
        } else if ($career == 14) {
            $careerStr = 'IN (14,15,16,17,18,19,20,21,22,23)';
        } else if ($career == 24) {
            $careerStr = 'IN (24,25,26,27,28,29)';
        } else if ($career == 30) {
            $careerStr = 'IN (30,31,32,33,34)';
        } else if ($career == 35) {
            $careerStr = 'IN (35,36,37,38,39)';
        } else if ($career == 40) {
            $careerStr = 'IN (40,41,42,43,44)';
        } else if ($career == 45) {
            $careerStr = 'IN (45,46,47,48,49,50,51,52,53,54,55,56)';
        } else if ($career == 57) {
            $careerStr = 'IN (57,58,59,60)';
        } else if ($career == 61) {
            $careerStr = 'IN (61,62,63,64,65,66)';
        } else if ($career == 67) {
            $careerStr = 'IN (67,68,69,70,71,72,73)';
        } else if ($career == 74) {
            $careerStr = 'IN (74,75,76)';
        } else if ($career == 77) {
            $careerStr = 'IN (77,78,79,80,81,82)';
        } else {
            $careerStr = "IN ({$career})";
        }

        $query = $this->db->query("SELECT * FROM `{$this->table}` AS r
  	                                INNER JOIN users AS u ON u.user_id = r.user_id
									WHERE `career` {$careerStr}
 									 ORDER BY `{$this->primary}` DESC 
                            	      LIMIT {$start}, {$limit}");
        return $query->result_array();
    }

    public function updateViewed($id) {
        return $this->db->query("UPDATE `{$this->table}` SET `viewed` = `viewed` + 1
  	                              WHERE `{$this->primary}` = '{$id}'");
    }

    function getRecruitmentById($id) {
        $query = $this->db->query("SELECT * FROM  `{$this->table}` AS r
    	                            INNER JOIN users AS u ON u.user_id = r.user_id
    	                            WHERE `{$this->primary}` = '{$id}'");
        return $query->row_array();
    }

    // Cập nhật giá trị rate cho hồ sơ
    function changeRate($recruitment_id, $data) {
        $this->db->where($this->primary, $recruitment_id);
        return $this->db->update($this->table, $data);
    }

    public function getHighestRateRecruitments($limit, $start) {
        $query = $this->db->query("SELECT * FROM `{$this->table}` AS r
  	                             INNER JOIN users AS u ON u.user_id = r.user_id
                            	     ORDER BY `rate` DESC 
                            	      LIMIT {$start}, {$limit}");

        return $query->result_array();
    }
    
    public function getNewestRecruitments($limit, $start) {
        $query = $this->db->query("SELECT * FROM `{$this->table}` AS r
  	                             INNER JOIN users AS u ON u.user_id = r.user_id
                            	     ORDER BY `recruitment_id` DESC 
                            	      LIMIT {$start}, {$limit}");

        return $query->result_array();
    }
}